﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;

public partial class AdminX_Product_SalesOff : System.Web.UI.UserControl
{
    #region Constants
    private DataTable mCateData = new DataTable();
    protected const string HOLIZOTAL_COLUMN_NAME = "HolizotalSort";
    protected const string VERTICAL_COLUMN_NAME = "VerticalSort";

    #endregion Constants
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.ShowMess();
            this.BindDataToDropCategory();
         
            this.BindDataToGrid(1);

        }
    }



    #region Bind DropdownList

    private void BindDataToDropCategory()
    {

        int nDefaultLang = Support.GetDefaultLanguage();
        ddlCategory.Items.Clear();
        DataTable objTable = new DataTable();
        ProductCategory objProductCategory = new ProductCategory();
        objTable = objProductCategory.Search();
        int nCount = objTable.Rows.Count;
        FlowCategory flow = new FlowCategory(objTable, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);
        flow.IDColumnName = "ProductCategoryID";
        flow.ParentIDColumName = "[Parent]";

        flow.SortColumnName = "ProductCategoryName";

        flow.SortColumnName = "ProductCategoryName";
        flow.ParentSortDirection = "ASC";
        if (flow.Execute(0))
        {
            mCateData = flow.DestinationData();
        }
        if (mCateData != null)
        {
            foreach (DataRow dr in mCateData.Rows)
            {
                if (dr != null)
                {
                    string sText = Convert.ToString(dr["ProductCategoryName"]);
                    Int16 iHolz = Convert.ToInt16(dr[HOLIZOTAL_COLUMN_NAME]);
                    textCreateMarginLeft(ref sText, iHolz);
                    ListItem item = new ListItem(sText, Convert.ToString(dr["ProductCategoryID"]));
                    ddlCategory.Items.Add(item);
                }
            }
            ddlCategory.Items.Insert(0, new ListItem("--------- Tất cả ---------", "0"));
        }
        else
        {
            ddlCategory.DataSource = mCateData;
            ddlCategory.DataTextField = "ProductCategoryName";

        }

        ddlCategory.DataValueField = "ProductCategoryID";

        ddlCategory.DataBind();
        //  Utl.SetNonSelectToControl(ddlCategory, "-- Select one --", "-- No Available --");

    }

    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }

  
    #endregion

    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = 10000;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLanguage]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion



    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nCurrentLanguage = Support.GetCurrentLanguage();
        int nDefaultLanguage = Support.GetDefaultLanguage();
        Products objProduct = new Products();
        DataTable dtb = new DataTable();
        int nCategory = ConvertData.ConvertToInt(ddlCategory.SelectedValue);
        int nTrademark = -1;
        int nMaterial = -1;
        double dPriceFrom = 0;
        double dPriceTo = 100000000;
        string sProductName = txtSearch.Text.Trim();
        int nStatus = -1;
        int nPageCount = 0;
        int nItemView = 0;
        int nItemID = 0;
        int isSaleOff = -1;
        int nPageSize = 10000;
        int nPercent = ConvertData.ConvertToInt(txtPercentDiscount.Text.Trim());
        int nStartDate = ConvertData.ConvertToInt(txtStartedDate.Text.Trim());
        int nExpDate = ConvertData.ConvertToInt(txtExpriedDate.Text.Trim());
        if (nStartDate > 0)
        {nStartDate = ConvertData.ConvertStringToIntDate(txtStartedDate.Text.Trim()); }
        if(nExpDate >0)
        {nExpDate = ConvertData.ConvertStringToIntDate(txtExpriedDate.Text.Trim());}
       
     
        dtb = objProduct.Search(nItemView,"", sProductName, nItemID,ConvertData.ConvertToString(nCategory),nTrademark, nMaterial, dPriceFrom, dPriceTo, isSaleOff,-1, nStatus,nPercent,nExpDate , nStartDate,nDefaultLanguage ,nCurrentLanguage ,  Pager1.CurrentIndex, nPageSize, ref nPageCount);
        dtgProductCategory.DataSource = dtb;
        dtgProductCategory.DataBind();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];

        Pager1.ItemCount = nPageCount;
        VisiblePaging(nPageCount);
        if (dtb.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;

        txtSearch.Focus();
        txtSearch.Text = ConvertData.RemoveHTMLTag(txtSearch.Text.Trim());

    }




    protected void dtgProductCategory_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblProductName = (Label)e.Item.FindControl("lblProductName");
            Label lblID = (Label)e.Item.FindControl("lblID");
            Label lblCategory = (Label)e.Item.FindControl("lblCategory");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");
            Label lblDiscountPercent= (Label)e.Item.FindControl("lblDiscountPercent");
            TextBox txtDiscountPrice =(TextBox)e.Item.FindControl("txtDiscountPrice");
            TextBox txtGStarrtedDate =(TextBox)e.Item.FindControl("txtStarrtedDate");
            TextBox txtGExpiredDate = (TextBox)e.Item.FindControl("txtExpiredDate");

            lblID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lblProductName.Text = "(" + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode")) + ")" +
                                    ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName"));
            lblCategory.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName"));
            lblPrice.Text = ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "Price")));
            lblDiscountPercent.Text = ConvertData.ConvertToString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "PercentSaleOff")));
            txtDiscountPrice.Text = ConvertData.ConvertToString(Support.FormatCurrency(ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "PriceSaleOff"))));
            int nStartDate = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "StartedDateSaleOff"));
            int nExpDate = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ExpriedDateSaleOff"));
            if (nStartDate > 0)
            {
                txtGStarrtedDate.Text = ConvertData.ConvertIntToDatetimeString(nStartDate); 
            }
            if (nExpDate > 0)
            {
                txtGExpiredDate.Text = ConvertData.ConvertIntToDatetimeString(nExpDate); 
            }
        }
    }

   

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        int nPercent = ConvertData.ConvertToInt(txtPercentDiscount.Text );
        int nStartDate =  ConvertData.ConvertStringToIntDate(txtStartedDate.Text);
        int nExpDate = ConvertData.ConvertStringToIntDate(txtExpriedDate.Text);
        int nCount = dtgProductCategory.Items.Count;
        for (int i = 0; i <= nCount-1; i++)
        {
            //TextBox txtDiscountPrice = (TextBox)dtgProductCategory.Items[i].FindControl("txtDiscountPrice");
            //TextBox txtStarrtedDate = (TextBox)dtgProductCategory.Items[i].FindControl("txtStarrtedDate");
            //TextBox txtExpiredDate = (TextBox)dtgProductCategory.Items[i].FindControl("txtExpiredDate");
            Label lblID = (Label)dtgProductCategory.Items[i].FindControl("lblID");
            Label lblPrice = (Label)dtgProductCategory.Items[i].FindControl("lblPrice");
            int nID = ConvertData.ConvertToInt(lblID.Text) ;
            Products obj = new Products();
            obj.LoadById(nID);

            obj.Data.ExpriedDateSaleOff = ConvertData.ConvertStringToIntDate(txtExpriedDate.Text);
            obj.Data.IsSalesOff  = 1;

            obj.Data.PercentSaleOff = ConvertData.ConvertToInt( txtPercentDiscount.Text);
            obj.Data.PriceSaleOff = ConvertData.ConvertToDouble(lblPrice.Text) - (ConvertData.ConvertToDouble(lblPrice.Text) * ConvertData.ConvertToInt(txtPercentDiscount.Text)) / 100;
            obj.Data.SalesOffDescription = "Khuyến mại giảm giá " + " " + txtPercentDiscount.Text + "%";
            obj.Data.StartedDateSaleOff = ConvertData.ConvertStringToIntDate(txtStartedDate.Text);
            obj.Data.ExpriedDateSaleOff  = ConvertData.ConvertStringToIntDate(txtExpriedDate.Text);

            obj.Update(nID);
            
        
        }
    }
}
